;-------------------------------------------------------------LICENSE--------------------------------------------------------------;
;                                                                                                                                  ;
;The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    ;
;and Message Passing Interface (MPI) parallelization.                                                                              ;
;                                                                                                                                  ;
;Copyright (C) 2012                                                                                                                ;
;Ronglin Jiang                                                                                                                     ;
;rljiang@ssc.net.cn                                                                                                                ;
;585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             ;
;                                                                                                                                  ;
;This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         ;
;as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             ;
;                                                                                                                                  ;
;This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    ;
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        ;
;                                                                                                                                  ;
;You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     ;
;Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   ;
;                                                                                                                                  ;
;-------------------------------------------------------------LICENSE--------------------------------------------------------------;

pro plot_map_3d_execution, event, part

   widget_control, event.top, get_uvalue = state, /no_copy

;----------------------------------------------------------------------------------------------------------------------------------|
;  destroy object when exit
;----------------------------------------------------------------------------------------------------------------------------------|
   if part eq 'destroy' then begin
      obj_destroy, obj_valid()
      delvart = temporary(state)
      widget_control, event.top, /destroy
      retall
   endif

;   if obj_valid(state.ocontainer) and state.ball_touch eq 0 then obj_destroy, state.ocontainer
;   if not obj_valid(state.ocontainer) then state.ocontainer = obj_new('idl_container')

   drawid = widget_info(event.top, find_by_uname = 'window')
   widget_control, drawid, get_value = owindow1

;---------------------------------------------------------------------------------------------------------------------------------|
;     Prepare
;---------------------------------------------------------------------------------------------------------------------------------|
;if state.reset_main eq 1 then begin
;  if state.logon eq 0 then begin
;    if state.cbname eq 'Density' then begin
;      var_tmp =   $
;      congrid((*state.ptr_var)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 0], state.nx, state.ny, state.nz, /minus_one, /interp)
;      (*state.ptr_varscl)[*, *, *, 0] = bytscl(temporary(var_tmp), min = state.minv, max = state.maxv)
;    endif
;    if state.cbname eq 'Gas pressure' then begin
;      var_tmp =   $
;      congrid((*state.ptr_var)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 1], state.nx, state.ny, state.nz, /minus_one, /interp)
;      (*state.ptr_varscl)[*, *, *, 1] = bytscl(temporary(var_tmp), min = state.minv, max = state.maxv)
;    endif
;    if state.cbname eq 'Temperature' then begin
;      var_tmp =   $
;      congrid((*state.ptr_var)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 2], state.nx, state.ny, state.nz, /minus_one, /interp)
;      (*state.ptr_varscl)[*, *, *, 2] = bytscl(temporary(var_tmp), min = state.minv, max = state.maxv)
;    endif
;    if state.cbname eq 'Absolute velocity' then begin
;      var_tmp =   $
;      congrid((*state.ptr_var)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 3], state.nx, state.ny, state.nz, /minus_one, /interp)
;      (*state.ptr_varscl)[*, *, *, 3] = bytscl(temporary(var_tmp), min = state.minv, max = state.maxv)
;    endif
;    if state.cbname eq 'Current density' then begin
;      var_tmp =   $
;      congrid((*state.ptr_var)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 4], state.nx, state.ny, state.nz, /minus_one, /interp)
;      (*state.ptr_varscl)[*, *, *, 4] = bytscl(temporary(var_tmp), min = state.minv, max = state.maxv)
;    endif
;  endif else begin
;    if state.cbname eq 'Density' then begin
;      var_tmp =   $
;      alog10(congrid((*state.ptr_var)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 0], state.nx, state.ny, state.nz, /minus_one, /interp))
;      (*state.ptr_varscl)[*, *, *, 0] = bytscl(temporary(var_tmp), min = alog10(state.minv), max = alog10(state.maxv))
;    endif
;    if state.cbname eq 'Gas pressure' then begin
;      var_tmp =   $
;      alog10(congrid((*state.ptr_var)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 1], state.nx, state.ny, state.nz, /minus_one, /interp))
;      (*state.ptr_varscl)[*, *, *, 1] = bytscl(temporary(var_tmp), min = alog10(state.minv), max = alog10(state.maxv))
;    endif
;    if state.cbname eq 'Temperature' then begin
;      var_tmp =   $
;      alog10(congrid((*state.ptr_var)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 2], state.nx, state.ny, state.nz, /minus_one, /interp))
;      (*state.ptr_varscl)[*, *, *, 2] = bytscl(temporary(var_tmp), min = alog10(state.minv), max = alog10(state.maxv))
;    endif
;    if state.cbname eq 'Absolute velocity' then begin
;      var_tmp =   $
;      alog10(congrid((*state.ptr_var)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 3], state.nx, state.ny, state.nz, /minus_one, /interp))
;      (*state.ptr_varscl)[*, *, *, 3] = bytscl(temporary(var_tmp), min = alog10(state.minv), max = alog10(state.maxv))
;    endif
;    if state.cbname eq 'Current density' then begin
;      var_tmp =   $
;      alog10(congrid((*state.ptr_var)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 4], state.nx, state.ny, state.nz, /minus_one, /interp))
;      (*state.ptr_varscl)[*, *, *, 4] = bytscl(temporary(var_tmp), min = alog10(state.minv), max = alog10(state.maxv))
;    endif
;  endelse
;endif

;if state.reset_other eq 1 then begin
;  (*state.ptr_vscl)[0, *, *, *] =   $
;  congrid((*state.ptr_v)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 0], state.nx, state.ny, state.nz, /minus_one, /interp)
;  (*state.ptr_vscl)[1, *, *, *] =   $
;  congrid((*state.ptr_v)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 1], state.nx, state.ny, state.nz, /minus_one, /interp)
;  (*state.ptr_vscl)[2, *, *, *] =   $
;  congrid((*state.ptr_v)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 2], state.nx, state.ny, state.nz, /minus_one, /interp)
;  
;  (*state.ptr_bscl)[0, *, *, *] =   $
;  congrid((*state.ptr_b)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 0], state.nx, state.ny, state.nz, /minus_one, /interp)
;  (*state.ptr_bscl)[1, *, *, *] =   $
;  congrid((*state.ptr_b)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 1], state.nx, state.ny, state.nz, /minus_one, /interp)
;  (*state.ptr_bscl)[2, *, *, *] =   $
;  congrid((*state.ptr_b)[state.xrlp:state.xrhp, state.yrlp:state.yrhp, state.zrlp:state.zrhp, 2], state.nx, state.ny, state.nz, /minus_one, /interp)

;  (*state.ptr_parscl)[0, *] = ((*state.ptr_par)[0, *]-state.x[0])/(max(state.x)-min(state.x))*state.nx
;  (*state.ptr_parscl)[1, *] = ((*state.ptr_par)[1, *]-state.y[0])/(max(state.y)-min(state.y))*state.ny
;  (*state.ptr_parscl)[2, *] = ((*state.ptr_par)[2, *]-state.z[0])/(max(state.z)-min(state.z))*state.nz
;endif

;if state.cbname eq 'Density' then var_id = 0
;if state.cbname eq 'Gas pressure' then var_id = 1
;if state.cbname eq 'Temperature' then var_id = 2
;if state.cbname eq 'Absolute velocity' then var_id = 3
;if state.cbname eq 'Current density' then var_id = 4

;xzero = pos_index(state.x, (max(state.x)+min(state.x))/2)
;yzero = pos_index(state.y, (max(state.y)+min(state.y))/2)
;zzero = pos_index(state.z, (max(state.z)+min(state.z))/2)

xr = [0.0d0, state.rangex] - state.zero_posx
yr = [0.0d0, state.rangey] - state.zero_posy
zr = [0.0d0, state.rangez] - state.zero_posz

xl = xr[1] - xr[0]
yl = yr[1] - yr[0]
zl = zr[1] - zr[0]

xl_normal = xl / max([xl, yl, zl]) * state.x_scaling
yl_normal = yl / max([xl, yl, zl]) * state.y_scaling
zl_normal = zl / max([xl, yl, zl]) * state.z_scaling

xc = [-xl_normal / 2, xl_normal / float(state.nx)]
yc = [-yl_normal / 2, yl_normal / float(state.ny)]
zc = [-zl_normal / 2, zl_normal / float(state.nz)]

xca = [-xl_normal / 2 - xr[0] * xl_normal / xl, xl_normal / xl]
yca = [-yl_normal / 2 - yr[0] * yl_normal / yl, yl_normal / yl]
zca = [-zl_normal / 2 - zr[0] * zl_normal / zl, zl_normal / zl]

xcb = [-xl_normal / 2, xl_normal]
ycb = [-yl_normal / 2, yl_normal]
zcb = [-zl_normal / 2, zl_normal]

xcf = [-xl_normal / 2, xl_normal / xl]
ycf = [-yl_normal / 2, yl_normal / yl]
zcf = [-zl_normal / 2, zl_normal / zl]

;----------------------------------------------------------------------------------------------------------------------|
;     Output time
;----------------------------------------------------------------------------------------------------------------------|
print, 'Time index: ', state.t_id

;if state.ball_touch eq 0 then begin
;  print
;  print, 'Plot figure at the time of ', strmid(strcompress(state.t[t_id]), 0, 6)
;endif

;if part eq 'rotatex' or part eq 'save' then begin
;  state.omodel->rotate, [1, 0, 0], state.anglex
;  state.omodel->getproperty, transform = model_matrix
;  state.omodel_axis->setproperty, transform = model_matrix
;  state.omodel_axis->translate, state.move_axis_x, state.move_axis_y, state.move_axis_z
;  state.omodel_axis->scale, 0.2, 0.2, 0.2
;endif
;if part eq 'rotatey' or part eq 'save' then begin
;  state.omodel->rotate, [0, 1, 0], state.angley
;  state.omodel->getproperty, transform = model_matrix
;  state.omodel_axis->setproperty, transform = model_matrix
;  state.omodel_axis->translate, state.move_axis_x, state.move_axis_y, state.move_axis_z
;  state.omodel_axis->scale, 0.2, 0.2, 0.2
;endif
;if part eq 'rotatez' or part eq 'save' then begin
;  state.omodel->rotate, [0, 0, 1], state.anglez
;  state.omodel->getproperty, transform = model_matrix
;  state.omodel_axis->setproperty, transform = model_matrix
;  state.omodel_axis->translate, state.move_axis_x, state.move_axis_y, state.move_axis_z
;  state.omodel_axis->scale, 0.2, 0.2, 0.2
;endif

;if part eq 'scaling' then state.omodel->scale, state.scaling, state.scaling, state.scaling

;;----------------------------------------------------------------------------------------------------------------------|
;;     magnetic field
;;----------------------------------------------------------------------------------------------------------------------|
;if state.show_b eq 1 and state.ball_touch eq 0 then begin
;  mintervalid = widget_info(event.top, find_by_uname = 'minterval')
;  widget_control, mintervalid, get_value = minterval

;  if obj_valid(state.magmodel) then obj_destroy, state.magmodel
;  state.magmodel = obj_new('idlgrmodel')
;  
;  nseeds = (state.mne-state.mns)/(fix(minterval))[0]+1
;  seeds = reform(state.par_track[*, state.mns:state.mne:fix(minterval), t_id], 3, nseeds)

;  seeds[0, *] = (seeds[0, *]-state.x[0])/xl*state.nx
;  seeds[1, *] = (seeds[1, *]-state.y[0])/yl*state.ny
;  seeds[2, *] = (seeds[2, *]-state.z[0])/zl*state.nz

;  for i = 0l, nseeds-1 do begin
;    bi = seeds[0, i]
;    bj = seeds[1, i]
;    bk = seeds[2, i]

;    if bi lt 0 then continue
;    if bj lt 0 then continue
;    if bk lt 0 then continue
;    if bi gt state.nx-1 then continue
;    if bj gt state.ny-1 then continue
;    if bk gt state.nz-1 then continue

;    particle_trace, *state.ptr_bscl, seeds[*, i], verts, conns, max_iterations = state.mstep, integration = 0,  $
;      anisotropy = [xl/state.nx, yl/state.ny, zl/state.nz];, max_stepsize = max([xl/state.nx, yl/state.ny, zl/state.nz])
;    dims = size(verts, /dimensions)
;    if((n_elements(dims) eq 2) and (dims[0] eq 3)) then begin
;      flowtube, temporary(verts), tverts, tconns, size = state.mt/10.0,  $
;        grids = [xl_normal/xl/state.x_scaling^2, yl_normal/yl/state.y_scaling^2, zl_normal/zl/state.z_scaling^2]
;      ntverts = n_elements(tverts)/3
;      vertx = reform(tverts[0, *], ntverts)/xl*state.nx
;      verty = reform(tverts[1, *], ntverts)/yl*state.ny
;      vertz = reform(tverts[2, *], ntverts)/zl*state.nz
;      vertcolors = bytscl(interpolate(*state.ptr_bt, vertx, verty, vertz), min = state.minb, max = state.maxb)
;      olinest = obj_new('idlgrpolygon', temporary(tverts), polygons = temporary(tconns),   $
;        shading = 1, emission = [10, 10, 45])
;      olinest->setproperty, xcoord_conv = xcf, ycoord_conv = ycf, zcoord_conv = zcf
;      olinest->setproperty, palette = state.opalette, vert_colors = vertcolors,style=2
;      state.magmodel->add, olinest
;    endif

;    particle_trace, -*state.ptr_bscl, seeds[*, i], verts, conns, max_iterations = state.mstep, integration = 0,  $
;      anisotropy = [xl/state.nx, yl/state.ny, zl/state.nz];, max_stepsize = max([xl/state.nx, yl/state.ny, zl/state.nz])
;    dims = size(verts, /dimensions)
;    if((n_elements(dims) eq 2) and (dims[0] eq 3)) then begin
;      flowtube, temporary(verts), tverts, tconns, size = state.mt/10.0,  $
;        grids = [xl_normal/xl/state.x_scaling^2, yl_normal/yl/state.y_scaling^2, zl_normal/zl/state.z_scaling^2]
;      ntverts = n_elements(tverts)/3
;      vertx = reform(tverts[0, *], ntverts)/xl*state.nx
;      verty = reform(tverts[1, *], ntverts)/yl*state.ny
;      vertz = reform(tverts[2, *], ntverts)/zl*state.nz
;      vertcolors = bytscl(interpolate(*state.ptr_bt, vertx, verty, vertz), min = state.minb, max = state.maxb)
;      olinest = obj_new('idlgrpolygon', temporary(tverts),  polygons = temporary(tconns),   $
;        shading = 1, emission = [10, 10, 45])
;      olinest->setproperty, xcoord_conv = xcf, ycoord_conv = ycf, zcoord_conv = zcf
;      olinest->setproperty, palette = state.opalette, vert_colors = vertcolors
;      state.magmodel->add, olinest
;    endif
;  endfor
;  state.omodel->add, state.magmodel, position = 0
;endif

;;----------------------------------------------------------------------------------------------------------------------|
;;     velocity lines
;;----------------------------------------------------------------------------------------------------------------------|
;if obj_valid(state.velmodel) then obj_destroy, state.velmodel
;state.oborderv->setproperty, hide = 1                                          ; if not,  the program will be very slow.
;if state.show_v eq 1 and state.ball_touch eq 0 then begin

;  state.velmodel = obj_new('idlgrmodel')

;  nseeds = 1l*state.vn^2
;  seeds = fltarr(3, nseeds)
;  osymbolsv = make_array(nseeds, /obj)
;  osymbolspolyv = make_array(nseeds, /obj)
;  seedi = 0l

;  state.osymbolpoly->getproperty, data = sym_verts, polygons = sym_conns

;  if state.velplaned eq 'x' then begin
;    i = -state.velplane
;    for jj = 1, state.vn do begin
;      j = double(jj)*state.ny/(state.vn+1)
;      for kk = 1, state.vn do begin
;        k = double(kk)*state.nz/(state.vn+1)
;        vel_vector = (*state.ptr_vscl)[*, i, j, k]*[xc[1], yc[1], zc[1]]*[state.nx/xl, state.ny/yl, state.nz/zl]
;        vel_vector = vel_vector/sqrt(total(vel_vector^2)+1d-12)
;        theta = asin(vel_vector[2])*180/!pi
;        phi = atan(vel_vector[1], vel_vector[0])*180/!pi
;        t3d, /reset
;        t3d, rotate = [0, -theta, phi]

;        osymbolspolyv[seedi] = obj_new('idlgrpolygon', data = vert_t3d(sym_verts), polygons = sym_conns, shading = 1)
;        osymbolsv[seedi] = obj_new('idlgrsymbol', osymbolspolyv[seedi])
;        osymbolsv[seedi]->setproperty, size = state.syms*(*state.ptr_vt)[i, j, k]/state.maxvel/[xc[1], yc[1], zc[1]]*median([xc[1], yc[1], zc[1]])
;        if (*state.ptr_vt)[i, j, k]/state.maxvel gt state.varrow then osymbolsv[seedi]->setproperty, size = state.syms*state.varrow/[xc[1], yc[1], zc[1]]*median([xc[1], yc[1], zc[1]])
;        seeds[0, seedi] = i
;        seeds[1, seedi] = j
;        seeds[2, seedi] = k
;        state.ocontainer->add, osymbolsv[seedi]
;        state.ocontainer->add, osymbolspolyv[seedi]
;        seedi = seedi+1
;      endfor
;    endfor

;    if seedi ne 0 then begin
;      osymbolsv = osymbolsv[0:seedi-1]
;      seeds = seeds[*, 0:seedi-1]
;      nseeds = seedi
;      lpoly = lindgen(nseeds)
;    endif

;    state.oborderv->setproperty, data =   $
;      [[-1.0/state.nx*state.velplane, 0, 0],   $
;      [-1.0/state.nx*state.velplane, 0, 1],   $
;      [-1.0/state.nx*state.velplane, 1, 1],   $
;      [-1.0/state.nx*state.velplane, 1, 0],   $
;      [-1.0/state.nx*state.velplane, 0, 0]], color = state.tc, linestyle = 2, thick = 2
;    state.oborderv->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, linestyle = 2, hide = 1-state.show_v
;  endif

;  if state.velplaned eq 'y' then begin
;    j = -state.velplane
;    for ii = 1, state.vn do begin
;      i = double(ii)*state.nx/(state.vn+1)
;      for kk = 1, state.vn do begin
;        k = double(kk)*state.nz/(state.vn+1)
;        vel_vector = (*state.ptr_vscl)[*, i, j, k]*[xc[1], yc[1], zc[1]]*[state.nx/xl, state.ny/yl, state.nz/zl]
;        vel_vector = vel_vector/sqrt(total(vel_vector^2)+1d-12)
;        theta = asin(vel_vector[2])*180/!pi
;        phi = atan(vel_vector[1], vel_vector[0])*180/!pi
;        t3d, /reset
;        t3d, rotate = [0, -theta, phi]
;        osymbolspolyv[seedi] = obj_new('idlgrpolygon', data = vert_t3d(sym_verts), polygons = sym_conns, shading = 1)
;        osymbolsv[seedi] = obj_new('idlgrsymbol', osymbolspolyv[seedi])
;        osymbolsv[seedi]->setproperty, size = state.syms*(*state.ptr_vt)[i, j, k]/state.maxvel/[xc[1], yc[1], zc[1]]*median([xc[1], yc[1], zc[1]])
;        if (*state.ptr_vt)[i, j, k]/state.maxvel gt state.varrow then osymbolsv[seedi]->setproperty, size = state.syms*state.varrow/[xc[1], yc[1], zc[1]]*median([xc[1], yc[1], zc[1]])
;        seeds[0, seedi] = i
;        seeds[1, seedi] = j
;        seeds[2, seedi] = k
;        state.ocontainer->add, osymbolsv[seedi]
;        state.ocontainer->add, osymbolspolyv[seedi]
;        seedi = seedi+1
;      endfor
;    endfor

;    if seedi ne 0 then begin
;      osymbolsv = osymbolsv[0:seedi-1]
;      seeds = seeds[*, 0:seedi-1]
;      nseeds = seedi
;      lpoly = lindgen(nseeds)
;    endif

;    state.oborderv->setproperty, data =   $
;      [[0, -1.0/state.ny*state.velplane, 0],   $
;      [0, -1.0/state.ny*state.velplane, 1],   $
;      [1, -1.0/state.ny*state.velplane, 1],   $
;      [1, -1.0/state.ny*state.velplane, 0],   $
;      [0, -1.0/state.ny*state.velplane, 0]], color = state.tc, linestyle = 2, thick = 2
;    state.oborderv->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, linestyle = 2, hide = 1-state.show_v
;  endif

;  if state.velplaned eq 'z' then begin
;    k = -state.velplane
;    for ii = 1, state.vn do begin
;      i = double(ii)*state.nx/(state.vn+1)
;      for jj = 1, state.vn do begin
;        j = double(jj)*state.ny/(state.vn+1)
;        vel_vector = (*state.ptr_vscl)[*, i, j, k]*[xc[1], yc[1], zc[1]]*[state.nx/xl, state.ny/yl, state.nz/zl]
;        vel_vector = vel_vector/sqrt(total(vel_vector^2)+1d-12)
;        theta = asin(vel_vector[2])*180/!pi
;        phi = atan(vel_vector[1], vel_vector[0])*180/!pi
;        t3d, /reset
;        t3d, rotate = [0, -theta, phi]
;        osymbolspolyv[seedi] = obj_new('idlgrpolygon', data = vert_t3d(sym_verts), polygons = sym_conns, shading = 1)
;        osymbolsv[seedi] = obj_new('idlgrsymbol', osymbolspolyv[seedi])
;        osymbolsv[seedi]->setproperty, size = state.syms*(*state.ptr_vt)[i, j, k]/state.maxvel/[xc[1], yc[1], zc[1]]*median([xc[1], yc[1], zc[1]])
;        if (*state.ptr_vt)[i, j, k]/state.maxvel gt state.varrow then osymbolsv[seedi]->setproperty, size = state.syms*state.varrow/[xc[1], yc[1], zc[1]]*median([xc[1], yc[1], zc[1]])
;        if (*state.ptr_vt)[i, j, k]/state.maxvel/state.varrow gt 1.0 then osymbolsv[seedi]->setproperty, size = state.syms
;        seeds[0, seedi] = i
;        seeds[1, seedi] = j
;        seeds[2, seedi] = k
;        state.ocontainer->add, osymbolsv[seedi]
;        state.ocontainer->add, osymbolspolyv[seedi]
;        seedi = seedi+1
;      endfor
;    endfor

;    if seedi ne 0 then begin
;      osymbolsv = osymbolsv[0:seedi-1]
;      seeds = seeds[*, 0:seedi-1]
;      nseeds = seedi
;      lpoly = lindgen(nseeds)
;    endif

; borderv = obj_new('idlgrpolyline', $
;  [0, 0, 1, 1, 0], $
;  [0, 1, 1, 0, 0], $
;  (state.nz+[state.velplane, state.velplane, state.velplane, state.velplane, state.velplane])*(1.0/state.nz), $
;  color = state.tc, thick = 2)

;    state.oborderv->setproperty, data =   $
;      [[0, 0, -1.0/state.nz*(state.nz+state.velplane)+1],   $
;      [0, 1, -1.0/state.nz*(state.nz+state.velplane)+1],   $
;      [1, 1, -1.0/state.nz*(state.nz+state.velplane)+1],   $
;      [1, 0, -1.0/state.nz*(state.nz+state.velplane)+1],   $
;      [0, 0, -1.0/state.nz*(state.nz+state.velplane)+1]], color = state.tc, linestyle = 2, thick = 2
;    state.oborderv->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, linestyle = 2, hide = 1-state.show_v
;  endif

;  if seedi ne 0 then begin
;    conns = lonarr(nseeds*3)
;    verts = fltarr(3, nseeds*2)
;    for seedi = 0, nseeds-1 do begin
;      conns[seedi*3] = 2
;      verts[*, seedi*2] = seeds[*, seedi]
;      conns[seedi*3+1] = seedi*2
;      verts[*, seedi*2+1] = (*state.ptr_vscl)[*, seeds[0, seedi], seeds[1, seedi], seeds[2, seedi]]*state.vstep*[state.nx/xl, state.ny/yl, state.nz/zl]+seeds[*, seedi]
;      conns[seedi*3+2] = seedi*2+1
;    endfor

;    olinetitlet = obj_new('idlgrpolyline')
;    olinetitlet->setproperty, data = verts, polylines = conns, thick = state.vt, label_objects = osymbolsv,  $
;      label_polylines = lpoly, label_offsets = 1, /label_use_vertex_color, /label_nogaps, use_label_orientation = 1
;    olinetitlet->setproperty, xcoord_conv = xc, ycoord_conv = yc, zcoord_conv = zc
;    nverts = n_elements(verts)/3
;    vertx = reform(verts[0, *], nverts)
;    verty = reform(verts[1, *], nverts)
;    vertz = reform(verts[2, *], nverts)
;    vertcolors = bytscl(sqrt((vertx-xzero)^2+(verty-yzero)^2+(vertz-zzero)^2))
;    olinetitlet->setproperty, palette = state.opalette, vert_colors = vertcolors
;    state.velmodel->add, olinetitlet
;    state.omodel->add, state.velmodel, position = 0
;  endif

;endif

;;----------------------------------------------------------------------------------------------------------------------|
;;     Test particle trajectories
;;----------------------------------------------------------------------------------------------------------------------|
;if state.show_par eq 1 and state.ball_touch eq 0 then begin
;  if obj_valid(state.parmodel) then obj_destroy, state.parmodel

;  pintervalid = widget_info(event.top, find_by_uname = 'pinterval')
;  widget_control, pintervalid, get_value = pinterval

;  nseeds = (state.pne-state.pns)/(fix(pinterval))[0]+1
;  pardatagon = lonarr(nseeds*(1+state.nt))
;  
;  state.parmodel = obj_new('idlgrmodel')
;  osymbolsp = make_array(nseeds, /obj)
;  osymbolspolyp = make_array(nseeds, /obj)
;  
;  state.osymbolpoly->getproperty, data = sym_verts, polygons = sym_conns

;  pli = 0
;  for pari = 0, nseeds-1 do begin
;    pardatagon[pli] = state.nt
;    pli = pli+1
;    for parj = 0, state.nt-1 do begin
;      pardatagon[pli] = state.pns+1l*state.par_num*parj+pari*(fix(pinterval))[0]
;      pli = pli+1
;    endfor
;  endfor

;  lpoly = lindgen(nseeds)
;  lenline = fltarr(state.nt, nseeds)+1d-16
;  label_ori = intarr(nseeds)
;  label_ori[*] = 1

;  for pari = 0, nseeds-1 do begin
;    for parj = 1, state.nt-1 do begin
;      lenline[parj, pari] = lenline[parj-1, pari]+  $
;        sqrt(total(((state.par_track[*, state.pns+pari, parj]-state.par_track[*, state.pns+pari, parj-1])*[state.nx/xl, state.ny/yl, state.nz/zl])^2))
;    endfor
;    if t_id gt 0 then begin
;      vector = (state.par_track[*, state.pns+pari, t_id]-state.par_track[*, state.pns+pari, t_id-1])*[xc[1], yc[1], zc[1]]*[state.nx/xl, state.ny/yl, state.nz/zl]
;      if total(abs(vector)) le 1d-12 then label_ori[pari] = 0
;      vector = vector/sqrt(total(vector^2)+1d-12)
;      theta = asin(vector[2])*180/!pi
;      phi = atan(vector[1], vector[0])*180/!pi
;      t3d, /reset
;      t3d, rotate = [0, -theta, phi]
;      osymbolspolyp[pari] = obj_new('idlgrpolygon', data = vert_t3d(sym_verts), polygons = sym_conns, shading = 1)
;      osymbolsp[pari] = obj_new('idlgrsymbol', osymbolspolyp[pari])
;      osymbolsp[pari]->setproperty, size = state.syms/[xc[1], yc[1], zc[1]]*median([xc[1], yc[1], zc[1]])
;      state.ocontainer->add, osymbolspolyp[pari]
;      state.ocontainer->add, osymbolsp[pari]
;    endif else begin
;      vector = (state.par_track[*, state.pns+pari, t_id+1]-state.par_track[*, state.pns+pari, t_id])*[xc[1], yc[1], zc[1]]*[state.nx/xl, state.ny/yl, state.nz/zl]
;      if total(abs(vector)) le 1d-12 then label_ori[pari] = 0
;      vector = vector/sqrt(total(vector^2)+1d-12)
;      theta = asin(vector[2])*180/!pi
;      phi = atan(vector[1], vector[0])*180/!pi
;      t3d, /reset
;      t3d, rotate = [0, -theta, phi]
;      osymbolspolyp[pari] = obj_new('idlgrpolygon', data = vert_t3d(sym_verts), polygons = sym_conns, shading = 1)
;      osymbolsp[pari] = obj_new('idlgrsymbol', osymbolspolyp[pari])
;      osymbolsp[pari]->setproperty, size = state.syms/[xc[1], yc[1], zc[1]]*median([xc[1], yc[1], zc[1]])
;      state.ocontainer->add, osymbolspolyp[pari]
;      state.ocontainer->add, osymbolsp[pari]
;    endelse
;  endfor

;  plineg = obj_new('idlgrpolyline', *state.ptr_parscl, polylines = pardatagon, thick = state.plinet, label_objects = osymbolsp,   $
;    label_offsets = lenline[t_id, *]/lenline[state.nt-1, *], label_polylines = lpoly, /label_use_vertex_color, /label_nogaps, use_label_orientation = label_ori)

;  npar = n_elements(*state.ptr_par)/3
;  vertx = reform((*state.ptr_par)[0, *], npar)
;  verty = reform((*state.ptr_par)[1, *], npar)
;  vertz = reform((*state.ptr_par)[2, *], npar)

;  vertcolors = bytscl(sqrt(vertx^2+verty^2+vertz^2))

;  plineg->setproperty, palette = state.opalette, vert_colors = vertcolors
;  plineg->setproperty, xcoord_conv = xc, ycoord_conv = yc, zcoord_conv = zc
;  state.parmodel->add, plineg
;  state.omodel->add, state.parmodel, position = 0
;endif

;;----------------------------------------------------------------------------------------------------------------------|
;;     Resistivity region
;;----------------------------------------------------------------------------------------------------------------------|
;state.borderr->setproperty, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca, hide = 1-state.show_res

;;----------------------------------------------------------------------------------------------------------------------|
;;     isosurface
;;----------------------------------------------------------------------------------------------------------------------|
;for iso_id = 0, 15 do begin
;  if state.isosurface_list[iso_id] ge 0 then begin
;    if state.isosurface_show[iso_id] eq 1 then begin
;      if state.ball_touch eq 0 then begin
;        isoval_id = widget_info(event.top, find_by_uname = 'iso_val'+strcompress(iso_id))
;        widget_control, isoval_id, get_value = iso_val
;        if state.logon eq 0 then iso_val = round((iso_val[0]-state.minv)/(state.maxv-state.minv)*255)  $
;                            else iso_val = round((alog10(iso_val[0])-alog10(state.minv))/(alog10(state.maxv)-alog10(state.minv))*255)
;        shade_volume, (*state.ptr_varscl)[*, *, *, var_id], iso_val, iso_verts, iso_conn
;        if n_elements(iso_verts) le 18 then begin
;          print, ' Warning: Too few points!'
;          state.isosurface_obj[iso_id]->setproperty, hide = 1
;          continue
;        endif

;        iso_verts = mesh_smooth(iso_verts, iso_conn, /fixed_edge_vertices)

;        vertcolors = intarr(state.nx, state.ny, state.nz)
;        for heighti = 0, state.nz-1 do vertcolors[*, *, heighti] = heighti
;        niso_verts = n_elements(iso_verts)/3
;        vertx = reform(iso_verts[0, *], niso_verts)
;        verty = reform(iso_verts[1, *], niso_verts)
;        vertz = reform(iso_verts[2, *], niso_verts)
;        vertcolors = bytscl(interpolate(vertcolors, vertx, verty, vertz), min = state.nz/5, max = state.nz-1)

;;----------------------------------------------------------------------------------------------------------------------|
;;    useful for a perforated isosurface
;;----------------------------------------------------------------------------------------------------------------------|
;;        image_size = 2
;;        level = 125
;;        m = bytarr(image_size, image_size, 4)+255b
;;        w = fix(interpolate([image_size-1, 3*float(image_size)/4], level/255.0))-1
;;        m[0:w, 0:w, 3] = 0
;;        texture_map = obj_new('IDLgrImage', m, interleave = 2)
;;        state.isosurface_obj[iso_id]->setproperty, texture_map = texture_map, texture_coord = [iso_verts[0, *], iso_verts[1, *]]
;;----------------------------------------------------------------------------------------------------------------------|
;        state.isosurface_obj[iso_id]->setproperty, palette = state.opalette, vert_colors = vertcolors, style = 2
;        state.isosurface_obj[iso_id]->setproperty, data = iso_verts, polygons = iso_conn, alpha_channel = float(state.isosurface_opac[iso_id])/100, hide = 0
;        state.isosurface_obj[iso_id]->setproperty, xcoord_conv = xc, ycoord_conv = yc, zcoord_conv = zc
;      endif else begin
;        state.isosurface_obj[iso_id]->setproperty, hide = state.ball_touch
;      endelse
;    endif
;  endif
;endfor

;;----------------------------------------------------------------------------------------------------------------------|
;;     slice
;;----------------------------------------------------------------------------------------------------------------------|
;for slice_id = 0, 15 do begin
;  if state.slice_list[slice_id] ge 0 then begin
;    if state.slice_show[slice_id] eq 1 then begin
;      if state.ball_touch eq 0 then begin
;        slice_size = state.nx+state.ny+state.nz
;        slice = extract_slice((*state.ptr_varscl)[*, *, *, var_id], slice_size, slice_size,   $
;          state.nx*state.slice_movex[slice_id]/100., state.ny*state.slice_movey[slice_id]/100., state.nz*state.slice_movez[slice_id]/100.,   $
;          state.slice_rotatex[slice_id]+1d-3, state.slice_rotatey[slice_id]+1d-3, 0,   $
;          vertices = slice_points, out_val = 0)
;        slice_points_index = where(slice_points[0, *, *] le state.nx and slice_points[0, *, *] ge 0 and  $
;                                 slice_points[1, *, *] le state.ny and slice_points[1, *, *] ge 0 and  $
;                                 slice_points[2, *, *] le state.nz and slice_points[2, *, *] ge 0)
;        slice_points = reform(slice_points, 3, 1l*slice_size*slice_size)
;        mesh_obj, 0, slice_verts, slice_conns, slice_points[*, slice_points_index]
;        iso_verts = mesh_smooth(slice_verts, slice_conns)
;        slice = reform(slice[slice_points_index], n_elements(slice_points_index))
;        state.oslice[slice_id]->setproperty, data = slice_verts, polygon = slice_conns, alpha_channel = float(state.slice_opac[slice_id])/100, style = 2, hide = 0
;        state.oslice[slice_id]->setproperty, palette = state.opalette, style = 2, vert_colors = slice
;        state.oslice[slice_id]->setproperty, xcoord_conv = xc, ycoord_conv = yc, zcoord_conv = zc
;      endif else begin
;        state.oslice[slice_id]->setproperty, hide = state.ball_touch
;      endelse
;    endif
;  endif
;endfor

;----------------------------------------------------------------------------------------------------------------------|
;     volume
;----------------------------------------------------------------------------------------------------------------------|
;if state.cuttingd eq 'x' then cutting_planes = [-1, 0, 0, state.nx+state.cutting]
;if state.cuttingd eq 'y' then cutting_planes = [0, 1, 0, state.cutting]
;if state.cuttingd eq 'z' then cutting_planes = [0, 0, -1, state.nz+state.cutting]
   for i_volume = 0, state.n_blocks_all[state.t_id] - 1 do begin

      xr_child = [min((*state.x_ptr)[*, i_volume]), max((*state.x_ptr)[*, i_volume])]
      yr_child = [min((*state.y_ptr)[*, i_volume]), max((*state.y_ptr)[*, i_volume])]
      zr_child = [min((*state.z_ptr)[*, i_volume]), max((*state.z_ptr)[*, i_volume])]

      xc_child = [xr_child[0], (xr_child[1] - xr_child[0]) / state.nx]
      yc_child = [yr_child[0], (yr_child[1] - yr_child[0]) / state.ny]
      zc_child = [zr_child[0], (zr_child[1] - zr_child[0]) / state.nz]
print, max((*state.data_scalar_byt_ptr)[*, *, *, i_volume])
print, min((*state.data_scalar_byt_ptr)[*, *, *, i_volume])
      (*state.ovolume_ptr)[i_volume] -> setproperty, xcoord_conv = xc_child, ycoord_conv = yc_child, zcoord_conv = zc_child,       $
         opacity_table0 = state.opac, data0 = (*state.data_scalar_byt_ptr)[*, *, *, i_volume]

   endfor

;   state.ovolume->setproperty, rgb_table0 = state.rgbtable, cutting_planes = cutting_planes,                                       $
;      hide = (1-state.show_var) or state.ball_touch
;   state.ovolume->setproperty, rgb_table0 = state.rgbtable,                                                                        $
;      hide = (1 - state.volume_switch)

;;----------------------------------------------------------------------------------------------------------------------|
;;     cutting border
;;----------------------------------------------------------------------------------------------------------------------|
;if state.cuttingd eq 'x' then begin
;  cutting_showx = 0
;  cutting_showy = 1
;  cutting_showz = 1
;endif
;if state.cuttingd eq 'y' then begin
;  cutting_showx = 1
;  cutting_showy = 0
;  cutting_showz = 1
;endif
;if state.cuttingd eq 'z' then begin
;  cutting_showx = 1
;  cutting_showy = 1
;  cutting_showz = 0
;endif

;state.bordercx->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb
;state.bordercy->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb
;state.bordercz->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb

;state.bordercx->setproperty, data =   $
;  [[(state.nx+state.cutting)*1.0/state.nx, 0, 0],   $
;  [(state.nx+state.cutting)*1.0/state.nx, 0, 1],   $
;  [(state.nx+state.cutting)*1.0/state.nx, 1, 1],   $
;  [(state.nx+state.cutting)*1.0/state.nx, 1, 0],   $
;  [(state.nx+state.cutting)*1.0/state.nx, 0, 0]],   $
;  color = state.tc, thick = 2, linestyle = 2, hide = abs(1-state.box_switch) or cutting_showx
;state.bordercy->setproperty, data =   $
;  [[0, -state.cutting*1.0/state.ny, 0],   $
;  [0, -state.cutting*1.0/state.ny, 1],   $
;  [1, -state.cutting*1.0/state.ny, 1],   $
;  [1, -state.cutting*1.0/state.ny, 0],   $
;  [0, -state.cutting*1.0/state.ny, 0]],   $
;  color = state.tc, thick = 2, linestyle = 2, hide = abs(1-state.box_switch) or cutting_showy
;state.bordercz->setproperty, data =   $
;  [[0, 0, (state.nz+state.cutting)*1.0/state.nz],   $
;  [0, 1, (state.nz+state.cutting)*1.0/state.nz],   $
;  [1, 1, (state.nz+state.cutting)*1.0/state.nz],   $
;  [1, 0, (state.nz+state.cutting)*1.0/state.nz],   $
;  [0, 0, (state.nz+state.cutting)*1.0/state.nz]],   $
;  color = state.tc, thick = 2, linestyle = 2, hide = abs(1-state.box_switch) or cutting_showz

;----------------------------------------------------------------------------------------------------------------------|
;     border
;----------------------------------------------------------------------------------------------------------------------|
   ref_point = [0, 0.0, 5.0]
   verts = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]]
   state.omodel -> getproperty, transform = trans_matrix

   verts_t = vert_t3d(verts, matrix = trans_matrix)
   dis = fltarr(8)
   for vertsi = 0, 7 do dis[vertsi] = total((verts_t[*, vertsi]-ref_point)^2)

   farthest_dis = max(dis, farthest_index)
   hide_point = verts[*, farthest_index]

   for bi = 0, 7 do for bj = 0, 2 do state.oborder[bi, bj]->setproperty, hide = 1
   for borderi = 0, 2 do state.oborder[farthest_index, borderi] -> setproperty, color = state.tc,                                  $
      xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, hide = abs(1 - state.box_switch), linestyle = 2
   display_index = hide_point[0]*1+(1-hide_point[1])*2+(1-hide_point[2])*4
   for borderi = 0, 2 do state.oborder[display_index, borderi] -> setproperty, color = state.tc,                                   $
      xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, hide = abs(1 - state.box_switch), linestyle = 0
   display_index = (1-hide_point[0])*1+hide_point[1]*2+(1-hide_point[2])*4
   for borderi = 0, 2 do state.oborder[display_index, borderi] -> setproperty, color = state.tc,                                   $
      xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, hide = abs(1 - state.box_switch), linestyle = 0
   display_index = (1-hide_point[0])*1+(1-hide_point[1])*2+hide_point[2]*4
   for borderi = 0, 2 do state.oborder[display_index, borderi] -> setproperty, color = state.tc,                                   $
      xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, hide = abs(1 - state.box_switch), linestyle = 0


;;----------------------------------------------------------------------------------------------------------------------|
;;     axis
;;----------------------------------------------------------------------------------------------------------------------|
;if obj_valid(state.oaxisx) then obj_destroy, state.oaxisx
;if obj_valid(state.oaxisy) then obj_destroy, state.oaxisy
;if obj_valid(state.oaxisz) then obj_destroy, state.oaxisz
;if obj_valid(state.textx1) then obj_destroy, state.textx1
;if obj_valid(state.textx2) then obj_destroy, state.textx2
;if obj_valid(state.texty1) then obj_destroy, state.texty1
;if obj_valid(state.texty2) then obj_destroy, state.texty2
;if obj_valid(state.textz1) then obj_destroy, state.textz1
;if obj_valid(state.textz2) then obj_destroy, state.textz2

;if state.box_switch eq 1 then begin
;  state.oaxisx = obj_new('idlgraxis', 0, /exact)
;  state.oaxisy = obj_new('idlgraxis', 1, /exact)
;  state.oaxisz = obj_new('idlgraxis', 2, /exact)
;  state.omodel->add, state.oaxisx
;  state.omodel->add, state.oaxisy
;  state.omodel->add, state.oaxisz
;  state.otitlex->setproperty, char_dimensions = [state.textsize3*xl/xl_normal, state.textsize3*yl/yl_normal],  $
;    font = state.ofont
;  state.otitley->setproperty, char_dimensions = [state.textsize3*yl/yl_normal, state.textsize3*xl/xl_normal],  $
;    font = state.ofont
;  state.otitlez->setproperty, char_dimensions = [state.textsize3*zl/zl_normal, state.textsize3*xl/xl_normal],  $
;    font = state.ofont
;  state.oaxisx->setproperty, title = state.otitlex, location = [xr[1]-xl*hide_point[0], yr[1]-yl*hide_point[1],  $
;    zr[1]-zl*(1-hide_point[2])], color = state.tc, major = state.x_tick_number, minor = 3,  $
;    ticklen = 0.03*yl, range = xr, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca
;  state.oaxisy->setproperty, title = state.otitley, location = [xr[1]-xl*(1-hide_point[0]), yr[1]-yl*hide_point[1],  $
;    zr[1]-zl*(1-hide_point[2])], color = state.tc, major = state.y_tick_number, minor = 3,  $
;    ticklen = 0.03*xl, range = yr, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca
;  state.oaxisz->setproperty, title = state.otitlez, location = [xr[1]-xl*(1-hide_point[0]), yr[1]-yl*hide_point[1],  $
;    zr[1]-zl*(1-hide_point[2])], color = state.tc, major = state.z_tick_number, minor = 3,  $
;    ticklen = 0.03*xl, range = zr, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca
;;----------------------------------------------------------------------------------------------------------------------|
;;     Note: reset the size of the tick label
;;----------------------------------------------------------------------------------------------------------------------|
;  state.oaxisx->getproperty, ticktext=ticktextx
;  ticktextx->setproperty, char_dimensions = [state.textsize3*xl/xl_normal, state.textsize3*yl/yl_normal]*0.66
;  state.oaxisy->getproperty, ticktext=ticktexty
;  ticktexty->setproperty, char_dimensions = [state.textsize3*xl/xl_normal, state.textsize3*yl/yl_normal]*0.66
;  state.oaxisz->getproperty, ticktext=ticktextz
;  ticktextz->setproperty, char_dimensions = [state.textsize3*xl/xl_normal, state.textsize3*yl/yl_normal]*0.66
;endif

;if state.box_switch eq 0 then begin
;  state.oaxisx = obj_new('idlgraxis', 0, /exact)
;  state.oaxisy = obj_new('idlgraxis', 1, /exact)
;  state.oaxisz = obj_new('idlgraxis', 2, /exact)
;  state.textx1 = obj_new('IDLgrText', 'x')
;  state.textx2 = obj_new('IDLgrText', 'x')
;  state.texty1 = obj_new('IDLgrText', 'y')
;  state.texty2 = obj_new('IDLgrText', 'y')
;  state.textz1 = obj_new('IDLgrText', 'z')
;  state.textz2 = obj_new('IDLgrText', 'z')
;  state.omodel->add, state.textx1
;  state.omodel->add, state.textx2
;  state.omodel->add, state.texty1
;  state.omodel->add, state.texty2
;  state.omodel->add, state.textz1
;  state.omodel->add, state.textz2
;  state.omodel->add, state.oaxisx
;  state.omodel->add, state.oaxisy
;  state.omodel->add, state.oaxisz
;  state.oaxisx->setproperty, color = [0, 0, 255], minor = 0, major = 0, thick = 2, range = xr, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca
;  state.oaxisy->setproperty, color = [0, 255, 0], minor = 0, major = 0, thick = 2, range = yr, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca
;  state.oaxisz->setproperty, color = [255, 0, 0], minor = 0, major = 0, thick = 2, range = zr, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca
;  state.textx1->setproperty, color = [0, 0, 255], locations = [xr[0]-xl/20, 0, 0], updir = [0, 0, 1], vertical_alignment = 0.5
;  state.textx2->setproperty, color = [0, 0, 255], locations = [xr[1]+xl/20, 0, 0], updir = [0, 0, 1], vertical_alignment = 0.5
;  state.texty1->setproperty, color = [0, 255, 0], locations = [0, yr[0]-yl/20, 0], updir = [0, 0, 1], vertical_alignment = 0.5
;  state.texty2->setproperty, color = [0, 255, 0], locations = [0, yr[1]+yl/20, 0], updir = [0, 0, 1], vertical_alignment = 0.5
;  state.textz1->setproperty, color = [255, 0, 0], locations = [0, 0, zr[0]-zl/20], updir = [0, 0, 1], vertical_alignment = 0.5
;  state.textz2->setproperty, color = [255, 0, 0], locations = [0, 0, zr[1]+zl/20], updir = [0, 0, 1], vertical_alignment = 0.5
;  state.textx1->setproperty, char_dimensions = [state.textsize3*xl, state.textsize3*zl]
;  state.textx2->setproperty, char_dimensions = [state.textsize3*xl, state.textsize3*zl]
;  state.texty1->setproperty, char_dimensions = [state.textsize3*xl, state.textsize3*zl]
;  state.texty2->setproperty, char_dimensions = [state.textsize3*xl, state.textsize3*zl]
;  state.textz1->setproperty, char_dimensions = [state.textsize3*xl, state.textsize3*zl]
;  state.textz2->setproperty, char_dimensions = [state.textsize3*xl, state.textsize3*zl]
;  state.textx1->setproperty, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca, font = state.ofont
;  state.textx2->setproperty, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca, font = state.ofont
;  state.texty1->setproperty, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca, font = state.ofont
;  state.texty2->setproperty, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca, font = state.ofont
;  state.textz1->setproperty, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca, font = state.ofont
;  state.textz2->setproperty, xcoord_conv = xca, ycoord_conv = yca, zcoord_conv = zca, font = state.ofont
;endif

;;----------------------------------------------------------------------------------------------------------------------|
;;     text
;;----------------------------------------------------------------------------------------------------------------------|
;state.otext_title->setproperty, font = state.ofont, char_dimensions = [state.textsize1, state.textsize1]
;state.otext_title->setproperty, string = 'Time = '+strmid(strcompress(state.t[t_id], /remove_all), 0, 4), color = state.tc
;state.otext_title->setproperty, xcoord_conv = [-0.2, 1.0], ycoord_conv = [0.81, 1.0]
;state.tmodel->reset
;state.tmodel->translate, state.movex+1, state.movey+1, 0

;;----------------------------------------------------------------------------------------------------------------------|
;;     colorbar
;;----------------------------------------------------------------------------------------------------------------------|
;if obj_valid(state.cmodel) then obj_destroy, state.cmodel
;;----------------------------------------------------------------------------------------------------------------------|
;; important: pass keywords by value,  for a member of structure
;;----------------------------------------------------------------------------------------------------------------------|

;state.cmodel = colorbar_obj(state.cmodel, rgbtable = state.rgbtable, range = [state.minv, state.maxv], color = state.tc, pos = [0.86, 0.25],   $
;  title_size = state.textsize2, title_c = state.cbname, log = state.logon, /text_dir_change, /vertical)
;state.cmodel->setproperty, hide = 1-state.show_colorbar
;state.cmodel->reset
;state.cmodel->translate, state.movex+1, state.movey+1, 0
;state.oview->add, state.cmodel, position = 1

;;----------------------------------------------------------------------------------------------------------------------|
;;     draw
;;----------------------------------------------------------------------------------------------------------------------|
;state.olite0->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, intensity = state.light0
;state.olite1->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, intensity = state.light1
;state.olite2->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, intensity = state.light1
;state.olite3->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, intensity = state.light1
;state.olite4->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, intensity = state.light1
;state.olite5->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, intensity = state.light1
;state.olite6->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, intensity = state.light1
;state.olite7->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, intensity = state.light1
;state.olite8->setproperty, xcoord_conv = xcb, ycoord_conv = ycb, zcoord_conv = zcb, intensity = state.light1

;state.oview->setproperty, color = state.bc, viewplane_rect = [state.movex, state.movey, 2, 2]
;if state.ball_touch ne 0 then owindow1->setproperty, quality = 1
;if state.ball_touch eq 0 then owindow1->setproperty, quality = 2
;for bi = 0, 7 do for bj = 0, 2 do state.oborder[bi, bj]->setproperty, thick = (state.win_sizex+state.win_sizey)/1000 > 1

   owindow1 -> draw, state.oview

;if part eq 'save' then begin
;  baseplot = widget_base(map = 0)
;  drawplot = widget_draw(baseplot, xsize = state.win_savex, ysize = state.win_savey, sensitive = 0, graphics_level = 2, renderer = 1)
;  widget_control, baseplot, /realize
;  widget_control, drawplot, get_value = owindow2
;  for bi = 0, 7 do for bj = 0, 2 do state.oborder[bi, bj]->setproperty, thick = (state.win_savex+state.win_savey)/640 > 1
;  owindow2->draw, state.oview
;  gimage = owindow2->read()
;  gimage->getproperty, data = image
;  
;  dotpos = strpos(strcompress(state.t(t_id), /remove_all), '.')
;  if state.show_colorbar eq 1 then begin
;    file_mkdir, strcompress(state.cbname, /remove_all)
;    cd, strcompress(state.cbname, /remove_all)
;    savetitle = strcompress(state.cbname, /remove_all)+'vol'+strcompress(state.show_var, /remove_all)+'mag'+  $
;      strcompress(state.show_b, /remove_all)+'vel'+strcompress(state.show_v, /remove_all)+  $
;      'time='+strmid('000'+strcompress(state.t(t_id), /remove_all), dotpos, 7)
;  endif else begin
;    file_mkdir, 'Trajectory3d'
;    cd, 'Trajectory3d'
;    savetitle = 'Trajectory3d'+strcompress(state.pns, /remove_all)+'to'+strcompress(state.pne, /remove_all)+  $
;      'time='+strmid('000'+strcompress(state.t(t_id), /remove_all), dotpos, 7)
;  endelse

;filesindex = 0
;filenamedefinition:
;filesname = file_search(count = filescount)
;for filesi = 0, filescount-1 do begin
;  if filesname[filesi] eq savetitle+'_'+strcompress(filesindex, /remove_all)+'.png' then begin
;    filesindex = filesindex+1
;    goto, filenamedefinition
;  endif
;endfor

;savetitle = savetitle+'_'+strcompress(filesindex, /remove_all)

;write_png, savetitle+'.png', image

;print, 'image has been saved.'
;widget_control, baseplot, /destroy
;endif

cd, state.figures_path

;state.reset_main = 0
;state.reset_other = 0

widget_control, event.top, set_uvalue = state, /no_copy

end
